package com.ruleengine.destination;

import lombok.extern.slf4j.Slf4j;
import org.pf4j.PluginManager;

import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class DestinationManager {
    private final Map<Integer, IDestinationProvider> availProviders;

    public DestinationManager(PluginManager pluginMgr) {
        this.availProviders = pluginMgr.getExtensions(IDestinationProvider.class)
                .stream().collect(Collectors.toMap(DestinationManager::mapToPluginId, e -> e));
        if (availProviders.isEmpty()) {
            log.warn("No destination provider plugin available, use DEV ONLY one instead");
            availProviders.put(0, new DevOnlyDestinationProvider());
        }
    }

    private static int mapToPluginId(IDestinationProvider provider) {
        String pluginClassName = provider.getClass().getName().toLowerCase();
        if (pluginClassName.contains("kafka")) {
            return 5;
        } else if (pluginClassName.contains("bifromq")) {
            return 4;
        }
        return -1;
    }

    public IDestinationProvider getDestinationProvider(int type) {
        return this.availProviders.get(type);
    }
}
